<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>单例设计模式：保障唯一实例的经典方案</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        }
        .code-block {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 12px;
            overflow: hidden;
        }
        .code-header {
            background: #2d2d2d;
            padding: 8px 16px;
            font-size: 12px;
            color: #888;
            border-bottom: 1px solid #3e3e3e;
        }
        pre {
            margin: 0;
            padding: 20px;
            overflow-x: auto;
        }
        .feature-icon {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
        }
        .section-title::before {
            content: '';
            display: inline-block;
            width: 4px;
            height: 24px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            margin-right: 12px;
            vertical-align: middle;
        }
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 1;
            font-weight: 700;
            margin-right: 8px;
            margin-top: -8px;
            color: #667eea;
            font-family: 'Noto Serif SC', serif;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6 text-center">
            <h1 class="text-5xl font-bold mb-4">单例设计模式</h1>
            <p class="text-2xl mb-8 opacity-90">保障唯一实例的经典方案</p>
            <div class="max-w-3xl mx-auto">
                <p class="text-lg leading-relaxed opacity-80">
                    在软件工程的世界里，有些对象注定是独一无二的。单例模式，作为23种设计模式中最简洁却又最实用的一种，
                    为我们提供了一种优雅的方式来确保类的唯一性，从数据库连接到配置管理，它无处不在。
                </p>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <div class="container mx-auto px-6 py-12">
        <!-- Introduction -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-8 card-hover">
            <p class="text-lg leading-relaxed text-gray-700">
                <span class="drop-cap">单</span>例设计模式是一种常见的创建型设计模式，它确保一个类只能创建一个实例，并提供了全局访问点来获取该实例。在Java应用中，单例模式有着广泛的应用，从数据库连接池、日志记录器到线程池管理器，都可以从单例模式中受益。
            </p>
        </div>

        <!-- Features Section -->
        <div class="mb-12">
            <h2 class="text-3xl font-bold mb-8 text-gray-800 section-title">核心特点</h2>
            <div class="grid md:grid-cols-2 gap-6">
                <div class="bg-white rounded-xl shadow-lg p-6 card-hover">
                    <div class="flex items-start">
                        <i class="fas fa-fingerprint text-4xl feature-icon mr-4"></i>
                        <div>
                            <h3 class="text-xl font-bold mb-2 text-gray-800">唯一实例</h3>
                            <p class="text-gray-600">单例模式保证一个类仅有一个实例，避免了多次实例化造成的资源浪费。</p>
                        </div>
                    </div>
                </div>
                <div class="bg-white rounded-xl shadow-lg p-6 card-hover">
                    <div class="flex items-start">
                        <i class="fas fa-globe text-4xl feature-icon mr-4"></i>
                        <div>
                            <h3 class="text-xl font-bold mb-2 text-gray-800">全局访问点</h3>
                            <p class="text-gray-600">通过提供一个全局访问方法，允许其他类在需要时获取该实例。</p>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Use Cases -->
        <div class="mb-12">
            <h2 class="text-3xl font-bold mb-8 text-gray-800 section-title">适用场景</h2>
            <div class="grid md:grid-cols-3 gap-6">
                <div class="bg-gradient-to-br from-purple-50 to-indigo-50 rounded-xl p-6 card-hover">
                    <i class="fas fa-share-alt text-3xl text-purple-600 mb-4"></i>
                    <h3 class="text-xl font-bold mb-2 text-gray-800">资源共享</h3>
                    <p class="text-gray-600">当多个对象需要共享同一个资源时，使用单例模式可以确保只有一个实例管理该资源，避免资源浪费。</p>
                </div>
                <div class="bg-gradient-to-br from-blue-50 to-cyan-50 rounded-xl p-6 card-hover">
                    <i class="fas fa-sliders-h text-3xl text-blue-600 mb-4"></i>
                    <h3 class="text-xl font-bold mb-2 text-gray-800">控制实例数量</h3>
                    <p class="text-gray-600">某些情况下，我们需要控制一个类只能拥有一个实例，如线程池、配置管理器等。</p>
                </div>
                <div class="bg-gradient-to-br from-green-50 to-emerald-50 rounded-xl p-6 card-hover">
                    <i class="fas fa-network-wired text-3xl text-green-600 mb-4"></i>
                    <h3 class="text-xl font-bold mb-2 text-gray-800">全局访问</h3>
                    <p class="text-gray-600">当一个类的实例在多个地方都需要被访问时，单例模式提供了一个简便的方式来获取实例。</p>
                </div>
            </div>
        </div>

        <!-- Visualization -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-12">
            <h2 class="text-3xl font-bold mb-8 text-gray-800 section-title">单例模式架构图</h2>
            <div class="mermaid">
                graph TD
                    A[客户端代码] -->|调用getInstance| B[单例类]
                    B --> C{实例是否存在?}
                    C -->|否| D[创建新实例]
                    C -->|是| E[返回现有实例]
                    D --> E
                    E --> F[唯一实例]
                    
                    style A fill:#e0e7ff,stroke:#6366f1,stroke-width:2px
                    style B fill:#ddd6fe,stroke:#8b5cf6,stroke-width:2px
                    style F fill:#c7d2fe,stroke:#6366f1,stroke-width:3px
            </div>
        </div>

        <!-- Implementation Methods -->
        <div class="mb-12">
            <h2 class="text-3xl font-bold mb-8 text-gray-800 section-title">实现方式</h2>
            <p class="text-lg text-gray-600 mb-8">以下是单例模式的几种常见实现方式：</p>

            <!-- Eager Initialization -->
            <div class="mb-8">
                <h3 class="text-2xl font-bold mb-4 text-gray-800">
                    <i class="fas fa-bolt text-yellow-500 mr-2"></i>饿汉式
                </h3>
                <p class="text-gray-600 mb-4">在类加载时即创建实例，避免了多线程问题。</p>
                <div class="code-block">
                    <div class="code-header">
                        <i class="fab fa-java mr-2"></i>Java
                    </div>
                    <pre><code>public class Singleton {
    private static Singleton instance = new Singleton();

    private Singleton() {}

    public static Singleton getInstance() {
        return instance;
    }
}</code></pre>
                </div>
            </div>

            <!-- Lazy Initialization -->
            <div class="mb-8">
                <h3 class="text-2xl font-bold mb-4 text-gray-800">
                    <i class="fas fa-bed text-blue-500 mr-2"></i>懒汉式
                </h3>
                <p class="text-gray-600 mb-4">实例只在第一次获取时创建，需要考虑线程安全问题。</p>
                <div class="code-block">
                    <div class="code-header">
                        <i class="fab fa-java mr-2"></i>Java
                    </div>
                    <pre><code>public class Singleton {
    private static Singleton instance;

    private Singleton() {}

    public static synchronized Singleton getInstance() {
        if (instance == null) {
            instance = new Singleton();
        }
        return instance;
    }
}</code></pre>
                </div>
            </div>

            <!-- Double-Checked Locking -->
            <div class="mb-8">
                <h3 class="text-2xl font-bold mb-4 text-gray-800">
                    <i class="fas fa-lock text-green-500 mr-2"></i>双重检查锁
                </h3>
                <p class="text-gray-600 mb-4">使用双重检查锁机制，在多线程情况下保证线程安全。</p>
                <div class="code-block">
                    <div class="code-header">
                        <i class="fab fa-java mr-2"></i>Java
                    </div>
                    <pre><code>public class Singleton {
    private static volatile Singleton instance;

    private Singleton() {}

    public static Singleton getInstance() {
        if (instance == null) {
            synchronized (Singleton.class) {
                if (instance == null) {
                    instance = new Singleton();
                }
            }
        }
        return instance;
    }
}</code></pre>
                </div>
            </div>

            <!-- Static Inner Class -->
            <div class="mb-8">
                <h3 class="text-2xl font-bold mb-4 text-gray-800">
                    <i class="fas fa-box text-purple-500 mr-2"></i>静态内部类
                </h3>
                <p class="text-gray-600 mb-4">利用类加载机制保证懒加载且线程安全。</p>
                <div class="code-block">
                    <div class="code-header">
                        <i class="fab fa-java mr-2"></i>Java
                    </div>
                    <pre><code>public class Singleton {
    private static class Holder {
        private static final Singleton INSTANCE = new Singleton();
    }

    private Singleton() {}

    public static Singleton getInstance() {
        return Holder.INSTANCE;
    }
}</code></pre>
                </div>
            </div>
        </div>

        <!-- Comparison Table -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-12">
            <h2 class="text-3xl font-bold mb-8 text-gray-800 section-title">实现方式对比</h2>
            <div class="overflow-x-auto">
                <table class="w-full">
                    <thead>
                        <tr class="border-b-2 border-gray-200">
                            <th class="text-left py-3 px-4 font-bold text-gray-700">实现方式</th>
                            <th class="text-left py-3 px-4 font-bold text-gray-700">线程安全</th>
                            <th class="text-left py-3 px-4 font-bold text-gray-700">延迟加载</th>
                            <th class="text-left py-3 px-4 font-bold text-gray-700">性能</th>
                            <th class="text-left py-3 px-4 font-bold text-gray-700">推荐度</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr class="border-b border-gray-100 hover:bg-gray-50">
                            <td class="py-3 px-4">饿汉式</td>
                            <td class="py-3 px-4"><i class="fas fa-check text-green-500"></i></td>
                            <td class="py-3 px-4